﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using game_for_test_helper.src.helper;

namespace game_for_test_helper.src.graphics
{
    class PointManger : DrawableGameComponent
    {
        private static VertexPositionColor[] pointList;
        private static int NumberOfPoints;
        private const int maxPoint = 4096;
        private BasicEffect basicEffect;

        private VertexBuffer vertexBuffer;
        private Matrix worldMatrix;
        private Matrix projectionMatrix;


        public PointManger(Game game)
            : base(game)
        {


        }
        public override void Initialize()
        {
            base.Initialize();
            NumberOfPoints = 0;
            pointList = new VertexPositionColor[maxPoint];
            basicEffect = new BasicEffect(GraphicsDevice, null);
            basicEffect.VertexColorEnabled = true;

            worldMatrix = GameStaticHolder.WorldMatrix;
            projectionMatrix = GameStaticHolder.ProjectionMatrix;

        }
        protected override void LoadContent()
        {
            base.LoadContent();
        }
        protected override void UnloadContent()
        {
            base.UnloadContent();
        }
        public static void DrawPoinStatict(Vector3 pos)
        {
            Color color = Color.White;
            VertexPositionColor vpc = new VertexPositionColor(pos, color);
            pointList[NumberOfPoints] = vpc;
            NumberOfPoints++;
        }
        public static void DrawPoinStatict(Vector3 pos,Color color)
        {
            VertexPositionColor vpc = new VertexPositionColor(pos, color);
            pointList[NumberOfPoints] = vpc;
            NumberOfPoints++;
        }
        public void DrawPoint(Vector3 pos)
        {
            Color color = Color.White;
            VertexPositionColor vpc = new VertexPositionColor(pos, color);
            pointList[NumberOfPoints] = vpc;
            NumberOfPoints++;

            vertexBuffer = new VertexBuffer(GraphicsDevice, VertexPositionColor.SizeInBytes * (pointList.Length), BufferUsage.None);

            // Set the vertex buffer data to the array of vertices.
            vertexBuffer.SetData<VertexPositionColor>(pointList);
        }
        public void DrawPoint(Vector3 pos, Color color)
        {
            VertexPositionColor vpc = new VertexPositionColor(pos, color);
            pointList[NumberOfPoints] = vpc;
            NumberOfPoints++;

            vertexBuffer = new VertexBuffer(GraphicsDevice, VertexPositionColor.SizeInBytes * (pointList.Length), BufferUsage.None);
            // Set the vertex buffer data to the array of vertices.
            vertexBuffer.SetData<VertexPositionColor>(pointList);
        }
        public override void Update(GameTime gameTime)
        {
            NumberOfPoints = 0;
            base.Update(gameTime);

            basicEffect.World = GameStaticHolder.WorldMatrix; ;
            basicEffect.View = SimpleCamera.ViewMatrix;
            basicEffect.Projection = GameStaticHolder.ProjectionMatrix;
            
        }
        public override void Draw(GameTime gameTime)
        {
            basicEffect.Begin(SaveStateMode.SaveState);
            GraphicsDevice.VertexDeclaration = GameStaticHolder.VertexDeclaration;
            GraphicsDevice.RenderState.PointSize = 10;
            GameStaticHolder.GraphicsDevice1.RenderState.DepthBufferEnable = true;

            foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
            {
                pass.Begin();
                if (NumberOfPoints > 0)
                    GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
                        PrimitiveType.PointList,
                        pointList,
                        0,  // index of the first vertex to draw
                        NumberOfPoints   // number of primitives
                    );
                //System.Console.WriteLine(NumberOfPoints);
                GraphicsDevice.RenderState.FillMode = FillMode.Solid;
                pass.End();
            }
            basicEffect.End();
            base.Draw(gameTime);
        }
    }
}
